package com.stars.services.impl.pshow;

import com.stars.dao.mapper.pshow.PShowOrdersMapper;
import com.stars.services.pshow.PShowOrdersConfigServices;
import com.stars.entity.pshow.PShowOrdersEntity;
import com.stars.dao.mapper.pshow.PShowOrdersMapper;

import org.springframework.stereotype.Service;

import com.stars.services.impl.BaseConfigServiceImpl;
import com.stars.entity.pshow.PShowOrdersGridJson;
import com.stars.common.maps.FastJsonUtils;
 import com.stars.common.ObjectCopyUtils;
import com.stars.common.base.BaseUUID;
import com.stars.services.pshow.PShowOrdersInfoConfigServices;
import com.stars.entity.pshow.PShowOrdersInfoEntity;
import com.stars.common.base.ResultMsg;
import java.util.List;
import com.stars.common.base.BaseConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang3.StringUtils;
import java.util.Date;

/**
 * @Title: 销售订单 业务逻辑
 * @author 肖伟
 * @date 2020-01-04
 * @version V1.0
 *
 */
@Service
public class PShowOrdersConfigServicesImpl extends BaseConfigServiceImpl<PShowOrdersMapper , PShowOrdersEntity> implements PShowOrdersConfigServices {
    @Autowired
    private PShowOrdersMapper pShowOrdersMapper;
	@Autowired
	private PShowOrdersInfoConfigServices pShowOrdersInfoConfigServicesImpl;

	@Override
	public ResultMsg addMain(PShowOrdersEntity entity,PShowOrdersGridJson gridJson)throws Exception{

		if(StringUtils.isNotBlank(gridJson.getAddJsonPShowOrdersInfo())){
			List<PShowOrdersInfoEntity> addList= FastJsonUtils.toList(gridJson.getAddJsonPShowOrdersInfo(), PShowOrdersInfoEntity.class);
			for(PShowOrdersInfoEntity add:addList){
                add.setPShowOrdersId(entity.getId());
                add.setId(BaseUUID.getUUID());
				pShowOrdersInfoConfigServicesImpl.insertSelective(add);
			}
		}
		if(StringUtils.isNotBlank(gridJson.getUpdateJsonPShowOrdersInfo())){
			List<PShowOrdersInfoEntity> updateList= FastJsonUtils.toList(gridJson.getUpdateJsonPShowOrdersInfo(),PShowOrdersInfoEntity.class);
			for(PShowOrdersInfoEntity update:updateList){
				pShowOrdersInfoConfigServicesImpl.updateSelectiveById(update);
			}
		}
		if(StringUtils.isNotBlank(gridJson.getDelJsonPShowOrdersInfo())){
			pShowOrdersInfoConfigServicesImpl.deleteByIds( gridJson.getDelJsonPShowOrdersInfo().split(BaseConstant.SPOT));
		}
        PShowOrdersEntity et =  pShowOrdersMapper.selectByPrimaryKey(entity.getId());
        if(null  == et ){
		    insertSelective(entity);
		}else {
		   ObjectCopyUtils.copyPropertiesIf(entity, et, BaseConstant.UPDATE);
           et.setLastUpdateDate(new Date());
           et.setLastUpdateId(entity.getCreateId());
           et.setLastUpdateName(entity.getCreateName());
           updateSelectiveById(et);
		}
        return ResultMsg.success();
	}
	@Override
    public ResultMsg delMainAndInfo(String[] ids) throws Exception {
        if(null != ids){
            deleteByIds(ids);
			return pShowOrdersInfoConfigServicesImpl.delByMainId(ids);
        }
        return ResultMsg.success();
    }



}